// components/taskDetails/taskDetails.js
import Toast from '@vant/weapp/toast/toast';
Page({
  data: {
    latitude: 30.59982,
    longitude: 114.3096,
    isShow: false,
    actions: [
      {
        name: '010-9987 0098',
        openType: 'share',
      },
    ],
    riShow: false,
    value: '',
    top: 0,
    main: '',
    activeNames: [],
    iconShow: false,
    nicList: [
      {
        url: 'https://img.yzcdn.cn/vant/leaf.jpg',
        name: '图片1',
      },
    ],
    bicList: [
      {
        url: 'https://img.yzcdn.cn/vant/leaf.jpg',
        name: '图片1',
      },
    ]
  },
  /**
   * 组件的方法列表
   */
  
  onLoad() {
    this.getAppInfo()
    this.getLocation()
  },
  async getLocation() {
    const res = await wx.getLocation() // 要提前申请权限
    console.log(res)
  },
  // 选择新的位置
  async chooseLocation() {
    // 调用小程序 API 获取新的位置
    const { latitude, longitude } = await wx.chooseLocation()
    // 获取新的位置附近的小区
    this.getPoint(latitude, longitude)

    console.log('起点位置:', latitude, longitude)
  },

  getPoint(latitude, longitude) {
    // 显示loading提示
    wx.showLoading({
      title: '正在加载...',
    })

    // 逆地址解析（根据经纬度来获取地址）
    QQMap.reverseGeocoder({
      location: [latitude, longitude].join(','),
      success: ({ result: { address } }) => {
        // console.log(address)
        // 数据数据
        this.setData({ address })
      },
    })
  },
  onChange(event) {
    // console.log(event);
    const n = event.detail
    if (n !== '') {
      this.setData({
        main: '取消任务'
      });
    } else {
      this.setData({
        main: ''
      });
    }
    this.setData({
      activeNames: event.detail,
    });
  },
  hIcon() {
    this.setData({ iconShow: !this.data.iconShow })
  },
  getAppInfo() {
    const res = wx.getSystemInfoSync()
    this.setData({ top: res.statusBarHeight })
  },
  onClickLeft() {
    wx.navigateBack({ delta: 1 })
    // wx.showToast({ title: '点击返回', icon: 'none' });
  },
  addPhone() {
    this.setData({ isShow: true })
  },
  onClose() {
    this.setData({ isShow: false });
  },
  onSelct() {
    this.setData({ isShow: false });
  },
  add(e) {
    console.log(e);
  },
  afterLead(event) {
    console.log(event);
    const n = event.mark.id
    const url = event.detail.file.url
    if (n) {
      this.upDate(url, n)
    } else {
      this.upDate(url, n)
    }
  },
  // 具体上传的操作
  upDate(url, m) {
    wx.uploadFile({
      url: wx.$http.baseURL + '/driver/files/imageUpload',
      filePath: url,
      name: 'file',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res)
        const n = JSON.parse(res.data)
        console.log(n)
        if (m) {
          this.data.nicList.push({ url: n.data })
          this.setData({ nicList: this.data.nicList })
        } else {
          this.data.bicList.push({ url: n.data })
          this.setData({ bicList: this.data.bicList })
        }
      }
    })
  },
  onDel(e) {
    console.log(e)
    const n = e.mark.id
    const idx = e.detail.index
    if (n) {
      this.data.nicList.splice(idx, 1)
      this.setData({ nicList: this.data.nicList })
    } else {
      this.data.bicList.splice(idx, 1)
      this.setData({ bicList: this.data.bicList })
    }
  },
  delivery(e){
    console.log(e);
    Toast({
      message: '交货成功',
      position:'bottom',
      onClose: () => {
      console.log('执行OnClose函数');
      }
    })
  },
  add_delivery(){
     wx.navigateTo({
       url: '/abnormal_pkg/pages/abnormal/index',
     })
  },
})
